'''
Created on Jul 14, 2012

@author: catapult
'''

import pygame
import gui
import button
import time
import pokemon
import move
import player
import random
import turnInfo
import gameStart

########################need to import csv###############
import csv

level = 1
trainers = []
wait = 0
maxLevel = 9

trainer = csv.reader(open("../data/trainers_pokemon.csv"), delimiter = ',', quotechar='/')
for row in trainer:
    trainers.append(row)

#gives trainers = [[trainer, pokemon, move, move, move, move, pokemon, move, etc.], [trainer, pokemon, move, move, move, move, pokemon, move, etc.]]
#only has trainers[1](monica),[2](caleb),[9](chris)

print("Program Started")
pygame.init()

pokemon.Pokemon(1, 1, 1, 1)
move.Move(3)

masterScreen = pygame.display.set_mode([gui.SCREEN_WIDTH, gui.SCREEN_HEIGHT])

print("Player Init called")
play = player.Player(0, gameStart.StartGame(masterScreen).run())
print("Player Init over")
opp = player.Player(1, trainers[level])

screen = gui.GUI(play, opp, masterScreen)

pokemon.tbox = screen.tbox
player.tbox = screen.tbox
move.tbox = screen.tbox
tbox = screen.tbox

#pokemon.dataInit()
battleStep = -1
tInfo = [0, 0]
first = 0
preStep = 0

forcePlayerPokeSwitch = False
forceOppPokeSwitch = False


while 1:
    if(battleStep != 1):
        for event in pygame.event.get():
            if(event.type == pygame.QUIT):
                pygame.quit()
                exit()
    if(battleStep == -1):
        battleStep = 0 # button to start a new battle
    elif(battleStep == 0):
        battleStep = 1 # run battle starting animation (34, 177, 76) = transparent
    elif(battleStep == 1):
        if(forceOppPokeSwitch == False):
            screen.bbox.setActive()
            tInfo[0] = 0
            for event in pygame.event.get():
                if event.type == pygame.MOUSEBUTTONDOWN:
                    print(pygame.mouse.get_pos())
                    tInfo[0] = screen.bbox.checkCollisionList(event.pos)
                if event.type == pygame.QUIT:
                    pygame.quit()
                    exit()
            if(type(tInfo[0]) != type(0)):
                battleStep = 2
                screen.bbox.inactive()
        elif(forceOppPokeSwitch == True):
            battleStep = 2
    elif(battleStep == 2):
        if(forceOppPokeSwitch == True):
            tInfo[0] = turnInfo.TurnInfo(0, 0)
            tInfo[1] = opp.chooseNewPoke()
        else:
            tInfo[1] = opp.chooseAction() # opponent chooses choice
        if(tInfo[0].mode == 2 or tInfo[0].mode == 3):
            first = 0 # if you are using an item or switching pokemon, you go first automatically
        elif(tInfo[1].mode == 2 or tInfo[1].mode == 3):
            first = 1 # if the other player is using an item or switching pokemon, they go first
        elif(play.pokemon[play.activePoke].move[tInfo[0].var].priority > opp.pokemon[opp.activePoke].move[tInfo[1].var].priority):
            first = 0 # if your move has a higher priority you go first
        elif(play.pokemon[play.activePoke].move[tInfo[0].var].priority < opp.pokemon[opp.activePoke].move[tInfo[1].var].priority):
            first = 1 # if your opponent's move has a higher priority they go first
        elif(play.pokemon[play.activePoke].speed_stat[0] > opp.pokemon[opp.activePoke].speed_stat[0]):
            first = 0 # if your speed is higher, you go first
        elif(play.pokemon[play.activePoke].speed_stat[0] < opp.pokemon[opp.activePoke].speed_stat[0]):
            first = 1 # if their speed is higher, they go first
        elif(forceOppPokeSwitch):
            first = 1
        else:
            first = random.randrange(0, 2) # if everything is equal, it's random
        battleStep = 3
        if(tInfo[1].mode == 1):
            if opp.pokemon[opp.activePoke].move[tInfo[1].var].PP_check() == False:
                battleStep = 2
            elif opp.pokemon[opp.activePoke].move[tInfo[1].var].PP_check() == False:
                battleStep = 2
    elif(battleStep == 3):
        if(first == 0):
            if(tInfo[0].mode == 1):
                print("It is the Player's turn")
                if(play.pokemon[play.activePoke].move[tInfo[0].var].use(play, opp) != False):
                    screen.bscreen.newAttack(0, play.pokemon[play.activePoke].HP_stat[0], opp.pokemon[opp.activePoke].HP_stat[0], tInfo[0].var)
            elif(tInfo[0].mode == 2):
                play.setActivePoke(tInfo[0].var)
                screen.bscreen.newPoke(0, play.pokemon[play.activePoke])
            elif(tInfo[0].mode == 3):
                pass
        if(first == 1):
            if(tInfo[1].mode == 1):
                print("It is the Opponent's turn")
                opp.pokemon[opp.activePoke].move[tInfo[1].var].use(opp, play)
                screen.bscreen.newAttack(0, play.pokemon[play.activePoke].HP_stat[0], opp.pokemon[opp.activePoke].HP_stat[0], tInfo[1].var, False)
            elif(tInfo[1].mode == 2):
                opp.setActivePoke(tInfo[1].var)
                screen.bscreen.newPoke(1, opp.pokemon[opp.activePoke])
            elif(tInfo[1].mode == 3):
                pass
        battleStep = 4
    elif(battleStep == 4):
        if(first == 0):
            if(tInfo[0].mode == 1):
                if(screen.bscreen.update()):
                    battleStep = 5
                    if(play.pokemon[play.activePoke].checkAlive() == False):
                        forcePlayerPokeSwitch = True
                       
                        battleStep = 7
                    if(opp.pokemon[opp.activePoke].checkAlive() == False):
                        forceOppPokeSwitch = True
                        
                        battleStep = 7
                    pygame.time.delay(500)
            elif(tInfo[0].mode == 2):
                if(screen.bscreen.update()):
                    battleStep = 5
                    if(forcePlayerPokeSwitch == True):
                        battleStep = 1
                        forcePlayerPokeSwitch = False
                    if(battleStep == 5):
                        pygame.time.delay(500)
            elif(tInfo[0].mode == 3):
                pass
        if(first == 1):
            if(tInfo[1].mode == 1):
                if(screen.bscreen.update()):
                    battleStep = 5
                    if(play.pokemon[play.activePoke].checkAlive() == False):
                        forcePlayerPokeSwitch = True
                       
                        battleStep = 7
                    if(opp.pokemon[opp.activePoke].checkAlive() == False):
                        forceOppPokeSwitch = True
                        
                        battleStep = 7
                    if(battleStep == 5):
                        pygame.time.delay(500)
            elif(tInfo[1].mode == 2):
                if(screen.bscreen.update()):
                    battleStep = 5
                    if(forceOppPokeSwitch == True):
                        battleStep = 1
                        forceOppPokeSwitch = False
                    if(battleStep == 5):
                        pygame.time.delay(500)
            elif(tInfo[1].mode == 3):
                pass
    elif(battleStep == 5):
        if(first == 1):
            if(tInfo[0].mode == 1):
                print("It is the Player's turn")
                if(play.pokemon[play.activePoke].move[tInfo[0].var].use(play, opp) != False):
                    screen.bscreen.newAttack(0, play.pokemon[play.activePoke].HP_stat[0], opp.pokemon[opp.activePoke].HP_stat[0], tInfo[0].var)
            elif(tInfo[0].mode == 2):
                play.setActivePoke(tInfo[0].var)
                screen.bscreen.newPoke(0, play.pokemon[play.activePoke])
            elif(tInfo[0].mode == 3):
                pass
        if(first == 0):
            if(tInfo[1].mode == 1):
                print("It is the Opponent's turn")
                opp.pokemon[opp.activePoke].move[tInfo[1].var].use(opp, play)
                screen.bscreen.newAttack(0, play.pokemon[play.activePoke].HP_stat[0], opp.pokemon[opp.activePoke].HP_stat[0], tInfo[1].var, False)
            elif(tInfo[1].mode == 2):
                opp.setActivePoke(tInfo[1].var)
                screen.bscreen.newPoke(1, opp.pokemon[opp.activePoke])
            elif(tInfo[1].mode == 3):
                pass
        battleStep = 6
    elif(battleStep == 6):
        if(first == 1):
            if(tInfo[0].mode == 1):
                if(screen.bscreen.update()):
                    battleStep = 7
                    if(play.pokemon[play.activePoke].checkAlive() == False):
                        forcePlayerPokeSwitch = True
                       
                        battleStep = 7
                    if(opp.pokemon[opp.activePoke].checkAlive() == False):
                        forceOppPokeSwitch = True
                        
                        battleStep = 7
            elif(tInfo[0].mode == 2):
                if(screen.bscreen.update()):
                    battleStep = 7
                    if(play.pokemon[play.activePoke].checkAlive() == False):
                        forcePlayerPokeSwitch = True
                       
                        battleStep = 7
                    if(opp.pokemon[opp.activePoke].checkAlive() == False):
                        forceOppPokeSwitch = True
                        
                        battleStep = 7
            elif(tInfo[0].mode == 3):
                pass
        if(first == 0):
            if(tInfo[1].mode == 1):
                if(screen.bscreen.update()):
                    battleStep = 7
                    if(play.pokemon[play.activePoke].checkAlive() == False):
                        forcePlayerPokeSwitch = True
                       
                        battleStep = 7
                    if(opp.pokemon[opp.activePoke].checkAlive() == False):
                        forceOppPokeSwitch = True
                        
                        battleStep = 7
            elif(tInfo[1].mode == 2):
                if(screen.bscreen.update()):
                    battleStep = 7
                    if(play.pokemon[play.activePoke].checkAlive() == False):
                        forcePlayerPokeSwitch = True
                       
                        battleStep = 7
                    if(opp.pokemon[opp.activePoke].checkAlive() == False):
                        forceOppPokeSwitch = True
                        
                        battleStep = 7
            elif(tInfo[1].mode == 3):
                pass
    elif(battleStep == 7):
        play.pokemon[play.activePoke].status_check()
        opp.pokemon[opp.activePoke].status_check()
        if(play.pokemon[play.activePoke].checkAlive() == False):
            forcePlayerPokeSwitch = True
            battleStep = 8
        if(opp.pokemon[opp.activePoke].checkAlive() == False):
            forceOppPokeSwitch = True
            battleStep = 8 # check for status damage/effects
        battleStep = 8
    elif(battleStep == 8):
        wait -= 1
        pygame.event.clear()
        print(play.checkLivingPokemon())
        if(play.checkLivingPokemon() == 0):
            screen.tbox.addMessage(30, "Player lost.")
            screen.tbox.addMessage(30, "Battle Resetting")
            wait = 150
            play.reset()
            opp.reset()
        elif(opp.checkLivingPokemon() == 0):
            screen.tbox.addMessage(30, "Player wins!")
            screen.tbox.addMessage(30, "Next opponent is " + trainers[level + 1][0])
            wait = 150
            if(level == maxLevel):
                screen.tbox.addMessage(30, "You have reached the end of the current content")
            else:
                level += 1
            play.reset()
            opp.newTeam(trainers[level])
        if(forcePlayerPokeSwitch == True):
            screen.bbox.forcePokeSwitch()
        if(wait == 1):
            screen.bscreen.newPoke(play.pokemon[play.activePoke], opp.pokemon[opp.activePoke])
        if(wait <= 0): 
            battleStep = 1 # check for battle completion and reset stuff if needed
    screen.display()
    pygame.time.delay(1000 // gui.FPS)



#battleScreenWidth = 256
#battleScreenHeight = 144
#battleScreen = pygame.Surface([battleScreenWidth, battleScreenHeight])
#backgroundImage = pygame.image.load("../images/BattleBackgrounds/background.png")
#secondBackgroundImage = pygame.image.load("../images/BattleBackgrounds/sprite5.png")
#battleScreen.blit(backgroundImage, [0,0])
#battleScreen.blit(secondBackgroundImage, [0,0])